package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayAdvertisement;
import org.itboys.mobile.entity.mongo.SundayAdvertisementZone;
import org.itboys.mobile.service.mongo.web.SundayAdvertisementService;
import org.itboys.mobile.service.mongo.web.SundayAdvertisementZoneService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/advertisement")
public class SundayAdvertisementController extends BaseController {

    @Autowired
    private SundayAdvertisementService advertisementService;
    @Autowired
    private SundayAdvertisementZoneService advertisementZoneService;


    /**---------------------------广告位信息--------------------------**/

    /**
     * 广告位主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/zone/index" )
    public  String zone_index(HttpServletRequest request,
                           Model model){
        return "/sunday/advertisement/zone/index";
    }

    /**
     * 广告位。列表，分页获取
     * @param request
     * @param response
     */
    @RequestMapping("/zone/select")
    public void zone_select(HttpServletRequest request,
                        HttpServletResponse response
    ) {
        try {
            PageResult<SundayAdvertisementZone> result=advertisementZoneService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 广告位。单个获取
     * @param request
     * @param response
     */
    @RequestMapping("/zone/findOne")
    public void zone_findOne(HttpServletRequest request,
                            HttpServletResponse response,
                             @RequestParam(value="id",required=true) Long id
    ) {
        try {
           SundayAdvertisementZone zone = advertisementZoneService.getById(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(zone, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 新增或修改广告位
     * @param request
     * @param response
     * @param zone
     */
    @RequestMapping("/zone/save")
    public  void zone_save(HttpServletRequest request,
                      HttpServletResponse response,
                      @ModelAttribute SundayAdvertisementZone zone
    ) {
        try {
            advertisementZoneService.saveZone(zone);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/advertisement/zone/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
   /**
     * 删除广告位
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/zone/delete")
    public void zone_delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id
    ) {
        try {
            advertisementZoneService.deleteZone(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/advertisement/zone/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**---------------------------广告信息--------------------------**/
    /**
     * 广告位
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index" )
    public  String index(HttpServletRequest request,
                              Model model,
                         @RequestParam(value="zoneKey",required=true) String zoneKey){
        model.addAttribute("zoneKey",zoneKey);
        return "/sunday/advertisement/index";
    }

    /**
     * 广告。列表，分页获取
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                            HttpServletResponse response
    ) {
        try {
            PageResult<SundayAdvertisement> result=advertisementService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 新增或修正页面
     * @param request
     * @param model
     * @param zoneKey
     * @return
     */
    @RequestMapping("/input" )
    public  String input(HttpServletRequest request,
                         Model model,
                         @RequestParam(value="id",required=true) Long id,
                         @RequestParam(value="zoneKey",required=false) String zoneKey){
        SundayAdvertisement advertisement=advertisementService.findOne(id,zoneKey);
        model.addAttribute("advertisement",advertisement);
        return "/sunday/advertisement/input";
    }

    /**
     * 新增或修改广告
     * @param request
     * @param response
     * @param Advertisement
     */
    @RequestMapping("/save")
    public  void save(HttpServletRequest request,
                           HttpServletResponse response,
                           @ModelAttribute SundayAdvertisement Advertisement,
                           @RequestParam(value="imageFile",required=false) MultipartFile imageFile,
                           @RequestParam(value="videoFile",required=false) MultipartFile videoFile

    ) {
        try {
            advertisementService.saveAdvertisement(Advertisement,imageFile,videoFile);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));

        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/advertisement/save------" + e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));;
        }
    }
    /**
     * 删除广告位
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value="id",required=true) Long id
    ) {
        try {
            advertisementService.delete(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/advertisement/delete------" + e.getMessage());
            }
            e.printStackTrace();

            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
